package com.jiayuge.server.controller;

import com.jiayuge.server.pojo.Admin;
import com.jiayuge.server.pojo.dto.ChatMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;

import java.time.LocalDate;

/**
 * 功能: websocket
 *
 * @Program: yeb
 * @Date: 2021-12-08 10:40
 * @Author Jiayu.Yang
 */
@Controller
public class WsController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/ws/chat")
    public void handleMsg(Authentication authentication, ChatMsg chatMsg){
        //获取当前登录用户
        Admin admin = (Admin) authentication.getPrincipal();
        //配置消息发送
        chatMsg.setFrom(admin.getUsername());
        chatMsg.setFromNickName(admin.getName());
        chatMsg.setDate(LocalDate.now());
        //发送消息
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);

    }
}
